package org.finesys.common.hazelcast.core.support;

import com.hazelcast.collection.IQueue;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.finesys.common.hazelcast.core.base.DistributedQueue;

import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
public class HazelcastQueue<T> implements DistributedQueue<T> {

    private final IQueue<T> queue;

    /**
     * 添加
     *
     * @param data 数据
     * @return true or false
     */
    @Override
    public boolean offer(T data) {
        return queue.offer(data);
    }

    /**
     * 检索并删除此队列的头，如果此队列为空，则返回null
     *
     * @param timeout  超时时间
     * @param timeUnit 时间单位
     * @return 数据
     */
    @SneakyThrows
    @Override
    public T poll(long timeout, TimeUnit timeUnit) {
        return queue.poll(timeout, timeUnit);
    }

    @Override
    public void clear() {
        queue.clear();
    }
}
